home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #1 / Amiga Plus 1999 #1.iso / System-Boost / Workbench / LFSystemBinder / compagnons / Registry / Examples / TestNotify.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-21  |  7.2 KB  |  216 lines

  1. /***********************************************************************
  2.                         TestNotify.c
  3.                         ©LFSoft 1995
  4.  
  5.       This code is fully a dommain public: Use it as you want...
  6.  
  7.      Test notifications from registery.library, and shared port mecanisme.
  8.  
  9.     24/07/1995  First version.
  10.  
  11.     /!\ Must be started from a CLI or a SHELL because of use of some stdio
  12.         output...
  13.  
  14.     Notes:
  15.         * All libraries (but registery.library) are openned and closed
  16.     automaticaly by DICE's autoinit feature. You must add some code for
  17.     others compilers.
  18.         * Note that we use the same class id for normal notification and for
  19.     NWF (with a different code), but you may use different ones...
  20.  
  21.     Testing:
  22.     ~~~~~~~~
  23.         + Start 2 shells.
  24.  
  25.         + Run TestNotify from the first:
  26.             Shell #7 STAT-RAM: > TestNotify
  27.  
  28.         + Test gadget's messages by clicking ... on the gadget !
  29.  
  30.         + Run TestResouce from the 2nd shell to test others messages:
  31.             Shell #5 STAT-RAM: > TestResource -n TST_RESOURCE
  32.                 This send a normal notification.
  33.                 "*TST Notif* Resource notification : Code 0" should be
  34.                 displayed in the first shell.
  35.  
  36.             Shell #5 STAT-RAM: >TestResource -l TST_RESOURCE
  37.                 This lock the resource.
  38.                 When you unlock the resouce (CTRL-C), the following message
  39.                 is displayed by TestNotify:
  40.                 "*TST Notif* Resource notification : Code 1"
  41.  
  42.         + Close all
  43.  
  44. ***********************************************************************/
  45.  
  46. #include <intuition/intuition.h>
  47. #include <proto/intuition.h>
  48. #include <proto/exec.h>
  49. #include <proto/registry.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52.  
  53. /*
  54.  *  Globals variables
  55.  */
  56. struct Library *RegistryBase=NULL;
  57. struct Window *win1=NULL;
  58.  
  59. // Note : We must initialise globales variables for cleanup() function
  60. ULONG   nclass=0;  // Notification class
  61. APTR    resource=NULL;
  62. APTR    notif=NULL;
  63. APTR    notifwf=NULL;   // Notify when free
  64.  
  65. /*
  66.  *  Window's definitions
  67.  *  (in old 1.3 style, but it still work under newer OS, tanks Amiga).
  68.  */
  69.  
  70. SHORT Border1_Vectors1[] = {
  71.         0,0,
  72.         138,0,
  73.         138,14,
  74.         0,14,
  75.         0,0
  76. };
  77. struct Border Border1_1 = {
  78.         -2,-1,  /* XY origin relative to container TopLeft */
  79.         3,0,JAM1,       /* front pen, back pen and drawmode */
  80.         5,      /* number of XY vectors */
  81.         Border1_Vectors1,        /* pointer to XY vectors */
  82.         NULL    /* next border in list */
  83. };
  84.  
  85. struct IntuiText IText1_1 = {
  86.         3,0,JAM2,       /* front and back text pens, drawmode and fill byte */
  87.         32,2,   /* XY origin relative to container TopLeft */
  88.         NULL,   /* font pointer or NULL for default */
  89.         "Gadget1",      /* pointer to text */
  90.         NULL    /* next IntuiText structure */
  91. };
  92.  
  93. struct Gadget gadw1_1 = {
  94.         NULL,   /* next gadget */
  95.         19,14,  /* origin XY of hit box relative to window TopLeft */
  96.         135,13, /* hit box width and height */
  97.         NULL,   /* gadget flags */
  98.         RELVERIFY,      /* activation flags */
  99.         BOOLGADGET,     /* gadget type flags */
  100.         (APTR)&Border1_1,        /* gadget border or image to be rendered */
  101.         NULL,   /* alternate imagery for selection */
  102.         &IText1_1,       /* first IntuiText structure */
  103.         NULL,   /* gadget mutual-exclude long word */
  104.         NULL,   /* SpecialInfo structure */
  105.         1,      /* user-definable data */
  106.         NULL    /* pointer to user-definable data */
  107. };
  108.  
  109. struct NewWindow NewWindowStructure1 = {
  110.         70,32,  /* window XY origin relative to TopLeft of screen */
  111.         189,34, /* window width and height */
  112.         0,1,    /* detail and block pens */
  113.         GADGETUP|CLOSEWINDOW,       /* IDCMP flags */
  114.         WINDOWSIZING+WINDOWDRAG+WINDOWDEPTH|WINDOWCLOSE,    /* other window flags */
  115.         &gadw1_1, /* first gadget in gadget list */
  116.         NULL,   /* custom CHECKMARK imagery */
  117.         "Notify example",     /* window title */
  118.         NULL,   /* custom screen pointer */
  119.         NULL,   /* custom bitmap */
  120.         5,5,    /* minimum width and height */
  121.         640,200,        /* maximum width and height */
  122.         WBENCHSCREEN    /* destination screen type */
  123. };
  124.  
  125. /*
  126.  *  Cleanup function
  127.  *
  128.  *  Note: Free objects in the reverse order than allocated.
  129.  *
  130.  */
  131.  
  132. void cleanup() {
  133.     if(RegistryBase){
  134.         RL_RemoveNotify(notif);             // Free notifications requests
  135.         RL_RemoveNotify(notifwf);
  136.         RL_UnRegisterResource(resource);    // Free our resource
  137.         RL_UnRegisterClass(nclass);         // Free our class
  138.         CloseLibrary(RegistryBase);         // and close the library
  139.     }
  140.     if(win1 !=NULL)CloseWindow(win1);
  141. }
  142.  
  143. void main(void)
  144. {
  145.     struct IntuiMessage *IM;
  146.     atexit(cleanup);
  147.  
  148.     if((win1 = (struct Window *)
  149.         OpenWindow(&NewWindowStructure1))==0)
  150.             exit(20);
  151.  
  152.     /*************************************************************************
  153.                                  All registry thing
  154.      ************************************************************************/
  155.         // Open the library
  156.     if(!(RegistryBase = OpenLibrary("registry.library",0))){
  157.         puts("Can't open registry.library");
  158.         exit(20);
  159.     }
  160.  
  161.         // Get an uniq Class id
  162.     if(!(nclass=RL_RegisterClass("TST_RES_NOTIF"))){
  163.         puts("Can't registring my class");
  164.         exit(20);
  165.     }
  166.  
  167.         // register our resource
  168.     if(!(resource=RL_RegisterResource("TST_RESOURCE"))){
  169.         puts("Can't register my resource");
  170.         exit(20);
  171.     }
  172.  
  173.         // Add a notification request
  174.     if(!(notif=RL_AddNotify(resource,RL_TP_NOTIF,win1->UserPort,nclass,0))){
  175.         puts("Can't add my notification request");
  176.         exit(20);
  177.     }
  178.  
  179.         // Add a notification request for free resource
  180.     if(!(notifwf=RL_AddNotify(resource,RL_TP_NWF,win1->UserPort,nclass,1))){
  181.         puts("Can't add my notification request (NWF)");
  182.         exit(20);
  183.     }
  184.  
  185.     RL_StartNotify(notif); // Enable notifications
  186.     RL_StartNotify(notifwf);
  187.  
  188.     /*************************************************************************
  189.                                  Standard messages loop
  190.      ************************************************************************/
  191.  
  192.     while(win1){
  193.         Wait(1L << win1->UserPort->mp_SigBit );   // Doesn't hurt multitaching
  194.         while (IM = (struct IntuiMessage *) GetMsg(win1->UserPort)){
  195.             switch(IM->Class){
  196.                 case CLOSEWINDOW :
  197.                     ReplyMsg((struct Message *)IM);
  198.                     puts("*TST Notif* window closed");
  199.                     if(win1 !=NULL){ CloseWindow(win1); win1=NULL; }
  200.                     break;
  201.                 case GADGETUP :
  202.                     puts("*TST Notif* Gadget clicked");
  203.                     ReplyMsg((struct Message *)IM);
  204.                     break;
  205.                 default:
  206.                     if(IM->Class == nclass){ // C compiler hate non-contant arg for case
  207.                         printf("*TST Notif* Resource notification : Code %d\n",IM->Code);
  208.                     }
  209.                     ReplyMsg((struct Message *)IM);
  210.                     break;
  211.             }
  212.             if(!win1) break;
  213.         }
  214.     }
  215. }
  216.